extends Node2D


const SPEED = 200
var maxX = 600
var minX = 0
var knight:Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	 knight = self.get_node("Knight")
	 maxX = maxX - knight.get_rect().size.x / 2
	 minX = minX + knight.get_rect().size.x / 2

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_key_pressed(KEY_D) or Input.is_key_pressed(KEY_RIGHT):
		moveKnightX(1,SPEED,delta)	
	elif Input.is_key_pressed(KEY_A) or Input.is_key_pressed(KEY_LEFT):
		moveKnightX(-1,SPEED,delta)
		
func moveKnightX(direction,speed,delta):
	if(direction == 0):
		return;
	knight.position += Vector2(SPEED,0)*delta*direction;
	
	if(knight.position.x > maxX):
		knight.position = Vector2(maxX,knight.position.y)
	elif(knight.position.x < minX):
		knight.position = Vector2(minX,knight.position.y)
		
	knight.scale = Vector2(direction,1)
	
